forum

Home / DeveloperSection / Forums / How to Upload file asynchronously using generic handler

How to Upload file asynchronously using generic handler

Pravesh Singh 2464 15-Jan-2015

I've used the code bellow to upload a file to server asynchronously:

HTML:

<form id="file_upload" action="UploadFile.ashx" target="upload-target" method="post" enctype="multipart/form-data" onsubmit="javascript:return uploadClicked();">

    <input type="file" id="newFile" name="newFile" />

    <input type="submit" />

    <iframe id="upload-target" name="upload-target"></iframe>

</form>

After the submit button clicked, the function uploadClicked() will be fired:

function uploadClicked() {
    if (condition == true)
        return true; //the form will submit
    else
        return false;
}

Now the generic handler UploadFile.ashx will save the file and return the result:

if (context.Request.Files.Count > 0)
{
    context.Request.Files["newFile"].SaveAs(HttpContext.Current.Server.MapPath("/Images/myFile.png"));
    response.Write("DONE");
}
else
{
    response.Write("FAILED");
}

This works well and the result will be showing in the iframe tag.

Is there anyway to get the result ("DONE" or "FAILED") in client-side like this ?

function uploadFinished()
{
     if ( response == "DONE" )
     {
          // show the result
     }
     else
     {
          // show error
     }
}

Please help me doing this without using JQuery.

Thanks in advance.


Updated on 15-Jan-2015

Can you answer this question?


Answer

1 Answers

Liked By